<template>
  <div class="setting-drawer">
    <a-drawer width="380" placement="right" @close="onClose" :closable="false" :visible="visible">
      <div class="setting-drawer-index-content">
        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">主题风格设置</h3>

          <div class="setting-drawer-index-blockChecbox">
            <a-tooltip>
              <template slot="title"> 暗色风格 </template>
              <div class="setting-drawer-index-item" @click="handleMenuTheme('dark')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/LCkqqYNmvBEbokSDscrm.svg" alt="dark" />
                <div class="setting-drawer-index-selectIcon" v-if="navTheme === 'dark'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>

            <a-tooltip>
              <template slot="title"> 亮色风格 </template>
              <div class="setting-drawer-index-item" @click="handleMenuTheme('light')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/jpRkZQMyYRryryPNtyIC.svg" alt="light" />
                <div class="setting-drawer-index-selectIcon" v-if="navTheme !== 'dark'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>
          </div>
        </div>

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">系统主题色</h3>

          <div style="height: 20px">
            <a-tooltip class="setting-drawer-theme-color-colorBlock" v-for="(item, index) in colorList" :key="index">
              <template slot="title">
                {{ item.key }}
              </template>
              <a-tag :color="item.color" @click="changeColor(item.color)">
                <a-icon type="check" v-if="item.color === primaryColor"></a-icon>
              </a-tag>
            </a-tooltip>
          </div>
        </div>
        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">顶栏主题</h3>

          <div style="height: 20px">
            <a-tooltip
              class="setting-drawer-theme-color-colorBlock"
              v-for="(item, index) in headBarColorList"
              :key="index"
            >
              <template slot="title">
                {{ item }}
              </template>
              <a-tag :color="item" @click="changeHeadBarColor(item)">
                <a-icon type="check" v-if="item === primaryColor"></a-icon>
              </a-tag>
            </a-tooltip>
          </div>
        </div>
        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">菜单主题</h3>

          <div style="height: 20px">
            <a-tooltip
              class="setting-drawer-theme-color-colorBlock"
              v-for="(item, index) in sideBarColorList"
              :key="index"
            >
              <template slot="title">
                {{ item }}
              </template>
              <a-tag :color="item" @click="changeSideBarColor(item)">
                <a-icon type="check" v-if="item === primaryColor"></a-icon>
              </a-tag>
            </a-tooltip>
          </div>
        </div>
        <a-divider />

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">导航模式</h3>

          <div class="setting-drawer-index-blockChecbox">
            <a-tooltip>
              <template slot="title"> 侧边栏导航 </template>
              <div class="setting-drawer-index-item" @click="handleLayout('sidemenu')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/JopDzEhOqwOjeNTXkoje.svg" alt="sidemenu" />
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode === 'sidemenu'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>

            <a-tooltip>
              <template slot="title"> 顶部栏导航 </template>
              <div class="setting-drawer-index-item" @click="handleLayout('topmenu')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg" alt="topmenu" />
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode === 'topmenu'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>

            <a-tooltip>
              <template slot="title"> 顶部拉通左侧显示菜单 </template>
              <div class="setting-drawer-index-item" @click="handleLayout('mixsidebar')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg" alt="mixsidebar" />
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode === 'mixsidebar'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>

            <a-tooltip>
              <template slot="title"> 左侧和顶部混合模式 </template>
              <div class="setting-drawer-index-item" @click="handleLayout('mix')">
                <img src="https://gw.alipayobjects.com/zos/rmsportal/KDNDBbriJhLwuqMoxcAr.svg" alt="mix" />
                <div class="setting-drawer-index-selectIcon" v-if="layoutMode === 'mix'">
                  <a-icon type="check" />
                </div>
              </div>
            </a-tooltip>
          </div>
          <div :style="{ marginTop: '24px' }">
            <a-list :split="false">
              <a-list-item>
                <a-tooltip slot="actions">
                  <template slot="title"> 该设定仅 [顶部栏导航] 时有效 </template>
                  <a-select
                    size="small"
                    style="width: 80px"
                    :defaultValue="contentWidth"
                    @change="handleContentWidthChange"
                  >
                    <a-select-option value="Fixed" v-if="layoutMode == 'topmenu'">固定</a-select-option>
                    <a-select-option value="Fluid">流式</a-select-option>
                  </a-select>
                </a-tooltip>
                <a-list-item-meta>
                  <div slot="title">内容区域宽度</div>
                </a-list-item-meta>
              </a-list-item>

              <a-list-item>
                <a-switch slot="actions" size="small" :defaultChecked="fixedHeader" @change="handleFixedHeader" />
                <a-list-item-meta>
                  <div slot="title">固定 Header</div>
                </a-list-item-meta>
              </a-list-item>

              <!-- <a-list-item>
                <a-switch
                  slot="actions"
                  size="small"
                  :disabled="!fixedHeader"
                  :defaultChecked="autoHideHeader"
                  @change="handleFixedHeaderHidden"
                />
                <a-list-item-meta>
                  <a-tooltip slot="title" placement="left">
                    <template slot="title">固定 Header 时可配置</template>
                    <div :style="{ opacity: !fixedHeader ? '0.5' : '1' }">下滑时隐藏 Header</div>
                  </a-tooltip>
                </a-list-item-meta>
              </a-list-item> -->

              <a-list-item>
                <a-switch
                  slot="actions"
                  size="small"
                  :disabled="layoutMode === 'topmenu'"
                  :defaultChecked="fixedSidebar"
                  @change="handleFixedSidebar"
                />
                <a-list-item-meta>
                  <div slot="title" :style="{ textDecoration: layoutMode === 'topmenu' ? 'line-through' : 'unset' }">
                    固定侧边菜单
                  </div>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </div>
        </div>
        <a-divider />

        <div :style="{ marginBottom: '24px' }">
          <h3 class="setting-drawer-index-title">其他设置</h3>
          <div>
            <a-list :split="false">
              <a-list-item>
                <a-switch slot="actions" size="small" :defaultChecked="colorWeak" @change="onColorWeak" />
                <a-list-item-meta>
                  <div slot="title">色弱模式</div>
                </a-list-item-meta>
              </a-list-item>
              <a-list-item>
                <a-switch slot="actions" size="small" :defaultChecked="multiTab" @change="onMultiTab" />
                <a-list-item-meta>
                  <div slot="title">多页签模式</div>
                </a-list-item-meta>
              </a-list-item>

              <a-list-item>
                <a-switch slot="actions" size="small" :defaultChecked="showBreadCrumb" @change="onShowBreadCrumb" />
                <a-list-item-meta>
                  <div slot="title">显示面包屑</div>
                </a-list-item-meta>
              </a-list-item>

              <a-list-item>
                <a-select
                  slot="actions"
                  size="small"
                  style="width: 80px"
                  :defaultValue="menuTriggerMode"
                  @change="handleMenuTriggerChange"
                >
                  <a-select-option value="headTop">导航栏</a-select-option>
                  <a-select-option value="menuBottom">菜单底部</a-select-option>
                </a-select>
                <a-list-item-meta>
                  <div slot="title">菜单伸缩图标位置</div>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </div>
        </div>

        <a-divider />
        <div :style="{ marginBottom: '24px' }">
          <a-button @click="doCopy" icon="copy" block>拷贝设置</a-button>
          <a-alert type="warning" :style="{ marginTop: '24px' }">
            <span slot="message">
              配置栏只在开发环境用于预览，生产环境不会展现，请手动修改配置文件。修改配置文件后，需要清空本地缓存和LocalStorage
              <a
                href="https://github.com/sendya/ant-design-pro-vue/blob/master/src/config/defaultSettings.js"
                target="_blank"
              >
                src/config/defaultSettings.js
              </a>
            </span>
          </a-alert>
        </div>
      </div>
      <div class="setting-drawer-index-handle" @click="toggle" slot="handle">
        <a-icon type="setting" v-if="!visible" />
        <a-icon type="close" v-else />
      </div>
    </a-drawer>
  </div>
</template>

<script>
import SettingItem from './SettingItem';
import { baseMixin } from '@/mixins/app-mixin';
import { colorList, HEADER_PRESET_BG_COLOR_LIST, SIDE_BAR_BG_COLOR_LIST } from './settingConfig';
import { updateTheme, updateColorWeak, updateThemeConfig } from '@/core/theme-setting';
import { TOGGLE_THEME_CONFIG } from '@/store/mutation-types';
import { defaultTheme, themeConfigs } from '@/config/theme.config';
import { colorUtils } from '@/utils';

export default {
  name: 'SettingDrawer',
  components: {
    SettingItem
  },
  mixins: [baseMixin],
  data() {
    return {
      visible: false,
      colorList,
      headBarColorList: HEADER_PRESET_BG_COLOR_LIST,
      sideBarColorList: SIDE_BAR_BG_COLOR_LIST
    };
  },
  watch: {},
  mounted() {},
  methods: {
    showDrawer() {
      this.visible = true;
    },
    onClose() {
      this.visible = false;
    },
    toggle() {
      this.visible = !this.visible;
    },
    onColorWeak(checked) {
      this.$store.dispatch('app/ToggleWeak', checked);
      updateColorWeak(checked);
    },
    onMultiTab(checked) {
      this.$store.dispatch('app/ToggleMultiTab', checked);
    },
    onShowBreadCrumb(checked) {
      this.$store.commit('app/SHOW_BREAD_CRUMB', checked);
    },

    /**
     * @description: 切换主题色
     * @param {*} theme
     * @return {*}
     */
    handleMenuTheme(theme) {
      let curConfig = Object.assign(themeConfigs[theme] || defaultTheme, {
        '@primary-color': this.primaryColor
      });

      this.$store.dispatch('app/ToggleTheme', theme);
      this.$store.commit(`app/${TOGGLE_THEME_CONFIG}`, curConfig);
      updateTheme(theme);
      updateThemeConfig(curConfig);
    },
    doCopy() {
      // get current settings from mixin or this.$store.state.app, pay attention to the property name
      const text = `export default {
  primaryColor: '${this.primaryColor}', // primary color of ant design
  navTheme: '${this.navTheme}', // theme for nav menu
  layout: '${this.layoutMode}', // nav menu position: sidemenu or topmenu
  contentWidth: '${this.contentWidth}', // layout of content: Fluid or Fixed, only works when layout is topmenu
  fixedHeader: ${this.fixedHeader}, // sticky header
  fixedSidebar: ${this.fixedSidebar}, // sticky siderbar
  autoHideHeader: ${this.autoHideHeader}, //  auto hide header
  colorWeak: ${this.colorWeak},
  multiTab: ${this.multiTab},
  production: process.env.NODE_ENV === 'production' && process.env.VUE_APP_PREVIEW !== 'true'
}`;
      this.$copyText(text)
        .then((message) => {
          console.log('copy', message);
          this.$message.success('复制完毕');
        })
        .catch((err) => {
          console.log('copy.err', err);
          this.$message.error('复制失败');
        });
    },
    handleLayout(mode) {
      this.$store.dispatch('app/ToggleLayoutMode', mode);
      // 因为顶部菜单不能固定左侧菜单栏，所以强制关闭
      this.handleFixedSidebar(false);
    },
    handleContentWidthChange(type) {
      this.$store.dispatch('app/ToggleContentWidth', type);
    },
    handleMenuTriggerChange(type) {
      this.$store.commit('app/MENU_TRIGGER_MODE', type);
    },

    /**
     * @description: 改变主题颜色配置
     * @param {*} color
     * @return {*}
     */
    changeColor(color) {
      if (this.primaryColor === color) return;

      let config = Object.assign(this.themeConfig, {
        '@primary-color': color
      });
      this.$store.dispatch('app/ToggleColor', color);
      this.$store.commit(`app/${TOGGLE_THEME_CONFIG}`, config);
      updateThemeConfig(config);
    },

    /**
     * @description: 改变导航栏颜色配置
     * @param {*} color
     * @return {*}
     */
    changeHeadBarColor(color) {
      let isDark = colorUtils.colorIsDark(color);
      let config = Object.assign(this.themeConfig, {
        '@layout-header-background': color,
        '@layout-header-font-color': isDark ? '@text-color-dark' : '@text-color'
      });

      this.$store.commit(`app/${TOGGLE_THEME_CONFIG}`, config);
      updateThemeConfig(config);
    },

    /**
     * @description: 改变菜单颜色配置
     * @param {*} color
     * @return {*}
     */
    changeSideBarColor(color) {
      let isDark = colorUtils.colorIsDark(color);
      let config = Object.assign(this.themeConfig, {
        '@menu-bg': color,
        '@menu-item-color': isDark ? '@text-color-dark' : '@text-color'
      });

      this.$store.commit(`app/${TOGGLE_THEME_CONFIG}`, config);
      updateThemeConfig(config);
    },

    handleFixedHeader(fixed) {
      this.$store.dispatch('app/ToggleFixedHeader', fixed);
    },

    handleFixedHeaderHidden(autoHidden) {
      this.$store.dispatch('app/ToggleFixedHeaderHidden', autoHidden);
    },

    handleFixedSidebar(fixed) {
      if (this.layoutMode === 'topmenu') {
        this.$store.dispatch('app/ToggleFixedSidebar', false);
        return;
      }

      this.$store.dispatch('app/ToggleFixedSidebar', fixed);
    }
  }
};
</script>

<style lang="less" scoped>
.setting-drawer-index-content {
  .setting-drawer-index-blockChecbox {
    display: flex;

    .setting-drawer-index-item {
      margin-right: 16px;
      position: relative;
      border-radius: 4px;
      cursor: pointer;

      img {
        width: 48px;
      }

      .setting-drawer-index-selectIcon {
        position: absolute;
        top: 0;
        right: 0;
        width: 100%;
        padding-top: 15px;
        padding-left: 24px;
        height: 100%;
        color: #1890ff;
        font-size: 14px;
        font-weight: 700;
      }
    }
  }
  .setting-drawer-theme-color-colorBlock {
    width: 20px;
    height: 20px;
    border-radius: 2px;
    float: left;
    cursor: pointer;
    margin-right: 8px;
    padding-left: 0px;
    padding-right: 0px;
    text-align: center;
    color: #fff;
    font-weight: 700;
    border: 1px solid @general-border-color;

    i {
      font-size: 14px;
    }
  }
}

.setting-drawer-index-handle {
  position: absolute;
  top: 240px;
  background: #1890ff;
  width: 48px;
  height: 48px;
  right: 380px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  pointer-events: auto;
  z-index: 1001;
  text-align: center;
  font-size: 16px;
  border-radius: 4px 0 0 4px;

  i {
    color: rgb(255, 255, 255);
    font-size: 20px;
  }
}
</style>
